package com.yjd.bi.statistic.control;

import com.yjd.bi.comm.utils.BiDateUtil;
import com.yjd.bi.report.model.BiDatabaseInfoModel;
import com.yjd.bi.report.service.IBiDatabaseInfoService;
import com.yjd.bi.report.util.BiUtil;
import com.yjd.comm.annotation.DateString;
import com.yjd.comm.annotation.ServiceRemoteProxy;
import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.rpm.RpmServiceKeyEnum;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.NumberUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by yt on 2016/12/15.
 */
@Controller
@Scope("prototype")
@RequestMapping("/spreadRelation")
public class SpreadRelationControl extends BaseControl {

    public final static String GET_SPREAD_USER_COUNT = "select count(DISTINCT(register)) as count from yjd_spread_relation where spread_rate =0.2 and addtime BETWEEN ? and ? ";

    //首投用户数
    public final static String GET_FIRST_RECH_COUNT = "SELECT count(distinct user_id) FROM ( SELECT i.addtime AS uptime, i.user_id FROM yjd_invest_first i, yjd_spread_relation u WHERE i.user_id = u.register AND u.spread_rate = 0.2 ) t where t.uptime BETWEEN ? AND ? ";

    //首投金额
    public final static String GET_FIRST_RECH_PRICE = "select ifnull(sum(price),0.00) from ( select min(uptime),price from ( SELECT * FROM ( SELECT i.addtime AS uptime, i.price, i.user_id FROM yjd_invest_first i, yjd_spread_relation u WHERE i.user_id = u.register AND u.spread_rate = 0.2 ) t WHERE t.uptime BETWEEN ? AND ? order by uptime asc)s group by user_id) t2 ";

    /**
     * 数据库信息服务
     */
    @ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
    private IBiDatabaseInfoService biDatabaseInfoService;

    @RequestMapping("/index")
    public String index ()throws Exception{
        request.setAttribute("start", "2013-12-21");
        request.setAttribute("now", BiDateUtil.initBiDate());
        return "spreadrelation.index";
    }


    /**
     * 基础数据-人人推广
     * @param end
     * @throws Exception
     */
    @RequestMapping("/summary")
    public void summary(@DateString String start,@DateString String end)throws  Exception{
        BiDatabaseInfoModel model = this.biDatabaseInfoService.getModelOne(FrameUtil.newHashMap("bs_type", 2), DbWREnums.READ);
        Long startDate = FrameUtil.getTime(DateUtil.parseDate(start,DateUtil.PATTERN_yyyyMMdd));
        Long endDate = FrameUtil.getTime(DateUtil.parseDate(end,DateUtil.PATTERN_yyyyMMdd))+86399;
        JdbcTemplate jt = BiUtil.getJdbcTemplate(model);
        int spread_user_count = jt.queryForObject(GET_SPREAD_USER_COUNT,new Object[] {startDate, endDate},Integer.class);
        int first_rech_count = jt.queryForObject(GET_FIRST_RECH_COUNT,new Object[] {startDate, endDate},Integer.class);
        double first_rech_price = jt.queryForObject(GET_FIRST_RECH_PRICE,new Object[] {startDate, endDate},Double.class);
        super.successData(FrameUtil.newHashMap("summaryVals", FrameUtil.newArrayList(
                NumberUtil.formatNumber(spread_user_count, NumberUtil.FORMAT_2),
                NumberUtil.formatNumber(first_rech_count, NumberUtil.FORMAT_2),
                NumberUtil.formatNumber(first_rech_price, NumberUtil.FORMAT_4))));
    }
}
